/*  
 *  ProtocolConsole.cs - Xboard protocol for playing against ACE.
 * 
 *  Copyright(c) 2007 Adam W Adair
 *
 *  This file is part of Adam's Chess Engine (ACE).
 *
 *  ACE is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *   
 *  ACE is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ACE; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Text;
using ACE.Board;
using ACE.Engine;

namespace ACE
{
    /// <summary>
    /// Base class for the protocols supported by ACE
    /// </summary>
    class ProtocolConsole
    {

        string lastReadLine="";
        protected ChessGame m_game;

        public ProtocolConsole(ChessGame game)
        {
            m_game = game;
            m_game.init();
        }

        protected string ReadLine()
        {
            lastReadLine = Console.ReadLine();
            Log.DefaultLog.LogDebug(" IN: " + lastReadLine);
            return lastReadLine;
        }

        public string LastCommand()
        {
            string[] szCmnd = lastReadLine.Split(" ".ToCharArray());
            return szCmnd[0];
        }

        public string LastReadLine()
        {
            return lastReadLine;
        }

        protected void WriteLine(string lineout)
        {
            Log.DefaultLog.LogDebug("OUT: " + lineout);
            Console.Write(lineout);
            Console.Write("\n");
            //Console.WriteLine(lineout);
        }

        public ChessGame Game
        {
            get { return m_game; }
            set { m_game = value; }
        }

        /// <summary>
        /// Reads a line from the console and returns the command from the line.
        /// </summary>
        /// <returns></returns>
        public string ReadCommand()
        {
            ReadLine();
            return LastCommand();
        }

        protected void Quit(int value)
        {
            System.Environment.Exit(value);
        }

        public void DebugOn()
        {
            Log.DefaultLog.Level = Log.LogLevel.LOG_ERROR | Log.LogLevel.LOG_WARNING |
                                        Log.LogLevel.LOG_INFO | Log.LogLevel.LOG_DEBUG;
            Log.DefaultLog.LogDebug("Debug mode turned on.");
        }
        public void DebugOff()
        {
            Log.DefaultLog.LogDebug("Debug mode turned off.");
            Log.DefaultLog.Level = Log.LogLevel.NO_LOG;            
        }

        public string []LastCommandArguments()
        {
            string cmd = LastReadLine();
            int indx = cmd.IndexOf(' ');
            if (indx > 0)
            {
                string args = cmd.Substring(indx+1);
                return args.Split(" ".ToCharArray());
            }
            return new string[0];
        }

    }
}
